package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.AuthorMapper;
import com.heima.article.service.AuthorService;
import com.heima.model.article.pojos.ApAuthor;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AuthorServiceImpl extends ServiceImpl<AuthorMapper, ApAuthor> implements AuthorService {

    /**
     * 根据UserId查询作者
     * @param id
     * @return
     */
    public ApAuthor findByUserId(Integer id){
        if(id == null ){
            new RuntimeException("没有参数");
        }
        LambdaQueryWrapper<ApAuthor> wrapper =new LambdaQueryWrapper();
        wrapper.eq(ApAuthor::getUserId,id);
        ApAuthor author = getOne(wrapper);
        if(author == null){
            return null;
        }
        return author;
    }

    /**
     * 保存作者
     * @param author
     * @return
     */
    public ResponseResult insert(ApAuthor author){
        if(author == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        author.setCreatedTime(new Date());
        save(author);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
